package com.onlyoffice.demo.ai.controller;

import com.onlyoffice.demo.onlyoffice.domain.R;
import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Flux;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试聊天Controller
 *
 * @author ZWX
 * @date 2025-07-16
 */
@RestController
@RequestMapping("/chat")
@RequiredArgsConstructor
public class ChatController {
    private final ChatClient openAiChatClient;

    /**
     * 聊天
     * @param prompt 提示词
     * @return 回答
     */
    @GetMapping(value = "/openai", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> chat(@RequestParam Long conversationId, @RequestParam String prompt) {
        return openAiChatClient.prompt()
            .user(prompt)
            .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId))
            .stream()
            .content();
        // String content = openAiChatClient.prompt()
        //     .user(prompt)
        //     .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId))
        //     .call()
        //     .content();
        // return R.ok(content);
    }
}
